package com.reconstructme.toggle;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.NetworkOnMainThreadException;

public class UDPAsyncTask extends AsyncTask<String, Void, Void> {

	@SuppressLint("NewApi")
	@Override
	protected Void doInBackground(String... params) {
    	try {
			String messageStr = params[0];
			String ip = params[1];
			int server_port = Integer.valueOf(params[2]);
			
			byte[] msg = messageStr.getBytes();
			
			InetAddress local = InetAddress.getByName(ip);
			DatagramPacket p = new DatagramPacket(msg, msg.length, local, server_port);
			DatagramSocket s = new DatagramSocket();
			
			s.send(p);
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NetworkOnMainThreadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
